package com.trickytriads.services.remote
{
	import com.adobe.serialization.json.JSON;
	import com.trickytriads.events.GetAllCardsEvent;
	import com.trickytriads.model.CardData;
	import com.trickytriads.services.FacebookService;
	import com.trickytriads.services.interfaces.I_AssetService;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;

	/**
	 * Remote asset service.  Pulls data from the server.
	 */
	[Event(name="getAllCardsDone", type="com.trickytriads.events.GetAllCardsEvent")]
	[ManagedEvents("getAllCardsDone")]
	public class AssetServiceImpl_Remote extends EventDispatcher implements I_AssetService
	{
		// Base URL of assets.
		private var _baseURL:String;
		// HTTPService
		[Inject]
		public var httpService:HTTPService;
		
		public function AssetServiceImpl_Remote(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		/**
		 * Concatenates the base URL and name of an image.
		 */
		public function getImageURL(name:String):String
		{
			return (BaseURL + name);
		}
		
		/**
		 * Function that handles requesting cards from the HTTPService.
		 */
		[MessageHandler(selector="getAllCardsRequest")]
		public function getAllCardsData(evt:GetAllCardsEvent):void
		{
			//trace("Initiating card request");
			var requestObject:Object = FacebookService.makeFacebookObject();			
			requestObject.ALLCARDS = "true";
			// Add event listeners
			httpService.addEventListener(ResultEvent.RESULT, getAllCardsDataResult);
			httpService.addEventListener(FaultEvent.FAULT, getAllCardsDataFault);
			httpService.send(requestObject);
		}
		
		/**
		 * Result handler for getAllCardsData.
		 */
		public function getAllCardsDataResult(evt:ResultEvent) : void
		{		
			// trace("Received data in getAllCardsDataResult");
			// Convert to generic object
			var resultObject:Object = JSON.decode(String(evt.result));
			if (resultObject.hasOwnProperty("I_AM_ERROR"))
			{
				Alert.show("Error object received in getAllCardsDataResult");
			}
			else
			{
				var cardArray:Array = new Array();
				// Add all of the cards to the cardArray
				for each (var cardObject:Object in resultObject)
				{
					// trace("Pushing card " + cardObject.card_id.toString() + " into array");					
					// Convert the "Object" to CardData
					var cardCardData:CardData = new CardData(cardObject.name, getImageURL(cardObject.imgname),
						cardObject.strengths as Array, cardObject.element, cardObject.card_id, cardObject.description,
						cardObject.cost, cardObject.level);
					// Push into the card array
					cardArray.push(cardCardData);						
				}
				var doneEvent:GetAllCardsEvent = new GetAllCardsEvent(GetAllCardsEvent.GETALLCARDS_DONE, cardArray);
				dispatchEvent(doneEvent);
			}
			// Remove event listeners for re-use
			httpService.removeEventListener(ResultEvent.RESULT, getAllCardsDataResult);
			httpService.removeEventListener(FaultEvent.FAULT, getAllCardsDataFault);
		}
		
		/**
		 * Fault handler for getAllCardsData.
		 */
		public function getAllCardsDataFault(evt:FaultEvent) : void
		{
			Alert.show("Fault in getAllCardsDataFault :" + evt.fault.message);
			// Remove event listeners for re-use
			httpService.removeEventListener(ResultEvent.RESULT, getAllCardsDataResult);
			httpService.removeEventListener(FaultEvent.FAULT, getAllCardsDataFault);				
		}
		
		
		public function get BaseURL ( ) : String { return _baseURL; }
		public function set BaseURL (v:String) : void { _baseURL = v; }
		
	}
}